﻿#region Opis pliku
//-----------------------------------------------------------------------------
// Ground.cs
// Autor: Jan Fraś
// 30.10.2012
//-----------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;


namespace pogra
{
    /// <summary>
    /// klasa szeregowca
    /// </summary>
    class Grunt:Ground
    {
        
        #region pola
        Texture2D grunttexture;
        #endregion

        #region inicjalizacja
        public Grunt(Vector2 pos, float rotation, int team, Texture2D texture, Texture2D particletexture, List<Unit> enemyteam, float velocity = 0) : 
            base(pos,rotation,team, enemyteam)
        {
            this.particletexture = particletexture;
            grunttexture = texture;
            hp = 100;
            maxhp = hp;
            attackPower = 2;
            upgradeQuant = 1;
            this.team = team;
            range = 300;
            shotrange = 300;
            shotInterval = 20;
            maxvelocity = 0.04f;
            this.velocity = velocity;
        }
        #endregion

        #region różna logika

        protected override bool attack()
        {
            float temp = -rotation + (float)((Math.Atan2(enemy.Position.Y - position.Y, enemy.Position.X - position.X) + Math.PI * 2) % (Math.PI * 2));
            while (temp > Math.PI)
                temp -= (float)Math.PI * 2;
            while (temp < -Math.PI)
                temp += (float)Math.PI * 2;

            if (base.attack() && Math.Abs(temp) < 0.1)
            {
                lastshottime = lifetime;
                Bullet tmp = new Bullet(attackPower, Rotation, enemy, this, particleSystem);
                bulletmanager.add(tmp);
                particleSystem.AddEmitter(new HelicopterShotEmitter(position + new Vector2((float)(5 * Math.Cos(rotation)), (float)(5 * Math.Sin(rotation))), particletexture, rotation));
            }
            else
            {
                if(velocity == 0)
                    rotation += temp;
            }
            return  base.attack();
        }

        public override int takehit(int hit, float attackrotation)
        {
            //particleSystem.AddEmitter(new HitEmitter(position - new Vector2((float)(20 * Math.Cos(attackrotation)), (float)(20 * Math.Sin(attackrotation))), attackrotation));
            return base.takehit(hit, attackrotation);
        }
        #endregion

        #region metody stanów
        protected override void IdleState(TimeSpan deltat)
        {
            if (findtarget())
                State = UnitState.Attacking;
            base.IdleState(deltat);
        }

        protected override void DeadState(TimeSpan deltat)
        {
            if (flags.BeingDaed == false)
            {
                particleSystem.AddEmitter(new HitEmitter(position,0));
            }
            base.DeadState(deltat);
        }

        protected override void TravelingState(TimeSpan deltat)
        {
            if(enemy!= null)
                attack();

            base.TravelingState(deltat);
        }

        protected override void AttackingState(TimeSpan deltat)
        {
            flags.Attacked = false;

            if (enemy != null)
            {
                if ((position - enemy.Position).Length() > range && mood == Moodes.Aggressive)
                {
                    setvalues();
                    move(deltat);
                }
                else
                {

                }
                attack();
                base.AttackingState(deltat);
            }
            else
                if(findtarget() == false)
                    State = UnitState.Idle;
        }

        #endregion

        #region metody draw i update
        public override void Draw(ExtendedSpriteBatch exSpriteBatch)
        {
            exSpriteBatch.Draw(grunttexture, Position, null, color, Rotation, new Vector2(grunttexture.Width, grunttexture.Height) / 2, 1, SpriteEffects.None, 0.95f);

            base.Draw(exSpriteBatch);
        }

        public override bool Update(TimeSpan deltat)
        {


            if (lifetime < 500 && State != UnitState.Dead)
            {
                move(deltat);
                lifetime += deltat.Milliseconds;
                return false;
            }
            base.Update(deltat);
            return true;
        }

        #endregion

    }
}
